import crypto from 'crypto'

export default{
    sha256(message, secret = '', encoding) {
        const hmac = crypto.createHmac('sha256', secret)
        return hmac.update(message).digest(encoding)
    },
    
    getHash(message, encoding = 'hex') {
        const hash = crypto.createHash('sha256')
        return hash.update(message).digest(encoding)
    },
    
    getDate(timestamp) {
        const date = new Date(timestamp * 1000)
        const year = date.getUTCFullYear()
        const month = ('0' + (date.getUTCMonth() + 1)).slice(-2)
        const day = ('0' + date.getUTCDate()).slice(-2)
        return `${year}-${month}-${day}`
    },
    
    getSign() {
        // 密钥参数
        const SECRET_ID = "AKID9C3f3C6lEXr24wa5OTKyObdS2NUM0Tkp"
        const SECRET_KEY = "vd9RUKwKVSBg2DAuRkpgIgqmTwNgngKj"
    
        const endpoint = "asr.tencentcloudapi.com"
        const service = "asr"
        const region = "ap-guangzhou"
        const action = "DescribeInstances"
        const version = "2017-03-12"
        //const timestamp = getTime()
        const timestamp = 1551113065
        //时间处理, 获取世界时间日期
        const date = this.getDate(timestamp)
    
        // ************* 步骤 1：拼接规范请求串 *************
        const signedHeaders = "content-type;host"
    
        const payload = "{\"Limit\": 1, \"Filters\": [{\"Values\": [\"\\u672a\\u547d\\u540d\"], \"Name\": \"instance-name\"}]}"
    
        const hashedRequestPayload = this.getHash(payload);
        const httpRequestMethod = "POST"
        const canonicalUri = "/"
        const canonicalQueryString = ""
        const canonicalHeaders = "content-type:application/json; charset=utf-8\n" + "host:" + endpoint + "\n"
    
        const canonicalRequest = httpRequestMethod + "\n"
                             + canonicalUri + "\n"
                             + canonicalQueryString + "\n"
                             + canonicalHeaders + "\n"
                             + signedHeaders + "\n"
                             + hashedRequestPayload
        // console.log(canonicalRequest)
    
        // ************* 步骤 2：拼接待签名字符串 *************
        const algorithm = "TC3-HMAC-SHA256"
        const hashedCanonicalRequest = this.getHash(canonicalRequest);
        const credentialScope = date + "/" + service + "/" + "tc3_request"
        const stringToSign = algorithm + "\n" +
                        timestamp + "\n" +
                        credentialScope + "\n" +
                        hashedCanonicalRequest
        // console.log(stringToSign)
    
        // ************* 步骤 3：计算签名 *************
        const kDate = this.sha256(date, 'TC3' + SECRET_KEY)
        const kService = this.sha256(service, kDate)
        const kSigning = this.sha256('tc3_request', kService)
        const signature = this.sha256(stringToSign, kSigning, 'hex')
        // console.log(signature)
    
        // ************* 步骤 4：拼接 Authorization *************
        const authorization = algorithm + " " +
                        "Credential=" + SECRET_ID + "/" + credentialScope + ", " +
                        "SignedHeaders=" + signedHeaders + ", " +
                        "Signature=" + signature
        // console.log(authorization)
        const curlcmd1 = {
            Authorization: authorization,
            'X-TC-Action': action,
            'X-TC-Timestamp': timestamp,
            'X-TC-Version': version,
            'X-TC-Region': region,
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Request-Method': 'GET POST PUT OPTION',
            'Access-Control-Request-Headers': 'Authorization',
            'Access-Control-Allow-Credentials': 'true',
            'X-TC-Token': '',
            payload: payload,
        }
    
        // const curlcmd = 'curl -X POST ' + "https://" + endpoint
        //                        + ' -H "Authorization: ' + authorization + '"'
        //                        + ' -H "Content-Type: application/json; charset=utf-8"'
        //                        + ' -H "Host: ' + endpoint + '"'
        //                        + ' -H "X-TC-Action: ' + action + '"'
        //                        + ' -H "X-TC-Timestamp: ' + timestamp.toString() + '"'
        //                        + ' -H "X-TC-Version: ' + version + '"'
        //                        + ' -H "X-TC-Region: ' + region + '"'
        //                        + " -d '" + payload + "'"
        return curlcmd1
    }
}

